/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dreamwar.view;

import dreamwar.controller.FeatureController;
import dreamwar.controller.GUIController;
import dreamwar.model.MyEntityManagerFactory;
import javax.swing.SwingUtilities;
import javax.swing.event.RowSorterEvent;
import javax.swing.event.RowSorterListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import lib.TableColumnAdjuster;
import org.jdesktop.observablecollections.ObservableCollections;

/**
 *
 * @author Berto_Romero
 */
public class FeatureListPanel extends javax.swing.JPanel {

    /**
     * Creates new form FeatureListJPanel
     */
    public FeatureListPanel() {
        initComponents();
        tca = new TableColumnAdjuster(featureTable);
        tca.setOnlyAdjustLarger(false);
        tca.adjustColumn(0);
        featureTable.getRowSorter().addRowSorterListener(new RowSorterListener() {

            @Override
            public void sorterChanged(RowSorterEvent e) {
                tca.adjustColumn(0);
            }
        });
        featureTable.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                tca.adjustColumn(0);
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : MyEntityManagerFactory.getInstance().getEntityManagerFactory().createEntityManager();
        featureQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT f FROM Feature f");
        featureList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : ObservableCollections.observableList(featureQuery.getResultList());
        featureTableScrollPane = new javax.swing.JScrollPane();
        featureTable = new javax.swing.JTable();
        keypadPanel = new javax.swing.JPanel();
        createButton = new javax.swing.JButton();
        modifyButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();

        featureController = FeatureController.getInstance(featureList);

        java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
        layout.columnWidths = new int[] {0};
        layout.rowHeights = new int[] {0, 5, 0};
        setLayout(layout);

        featureTableScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        featureTable.setAutoCreateRowSorter(true);
        featureTable.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, featureList, featureTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${featureName}"));
        columnBinding.setColumnName("Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${featureDescription}"));
        columnBinding.setColumnName("Descripción");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        featureTable.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                featureTableComponentResized(evt);
            }
        });
        featureTable.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                featureTableComponentAdded(evt);
            }
        });
        featureTableScrollPane.setViewportView(featureTable);
        if (featureTable.getColumnModel().getColumnCount() > 0) {
            featureTable.getColumnModel().getColumn(0).setResizable(false);
            featureTable.getColumnModel().getColumn(1).setResizable(false);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(25, 25, 0, 25);
        add(featureTableScrollPane, gridBagConstraints);

        java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();
        jPanel1Layout.columnWidths = new int[] {0, 5, 0, 5, 0};
        jPanel1Layout.rowHeights = new int[] {0};
        keypadPanel.setLayout(jPanel1Layout);

        createButton.setText("Crear");
        createButton.setMaximumSize(new java.awt.Dimension(75, 23));
        createButton.setMinimumSize(new java.awt.Dimension(75, 23));
        createButton.setPreferredSize(new java.awt.Dimension(75, 23));
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(createButton, gridBagConstraints);

        modifyButton.setText("Modificar");
        modifyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(modifyButton, gridBagConstraints);

        deleteButton.setText("Borrar");
        deleteButton.setMaximumSize(new java.awt.Dimension(75, 23));
        deleteButton.setMinimumSize(new java.awt.Dimension(75, 23));
        deleteButton.setPreferredSize(new java.awt.Dimension(75, 23));
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(deleteButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 25, 45);
        add(keypadPanel, gridBagConstraints);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyButtonActionPerformed
        if (featureTable.getSelectedRow() >= 0) {
            int idFeature = getIdFeatureFromJTable1();
            GUIController.getInstance().prepareModifyFeatureJPanel(featureList.get(featureTable.getSelectedRow()));
            GUIController.getInstance().changeMainFrameCard(GUIController.MODIFY_FEATURE_PANEL);
        }
    }//GEN-LAST:event_modifyButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        if (featureTable.getSelectedRow() >= 0) {
            int idFeature = getIdFeatureFromJTable1();
            featureController.deleteFeature(this, idFeature);
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        GUIController.getInstance().changeMainFrameCard(GUIController.CREATE_FEATURE_PANEL);
    }//GEN-LAST:event_createButtonActionPerformed

    private void featureTableComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_featureTableComponentAdded
        tca.adjustColumn(0);
    }//GEN-LAST:event_featureTableComponentAdded

    private void featureTableComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_featureTableComponentResized
        tca.adjustColumn(0);
    }//GEN-LAST:event_featureTableComponentResized

    private int getIdFeatureFromJTable1() {
        return featureList.get(featureTable.convertRowIndexToModel(featureTable.getSelectedRow())).getIdFeature();
    }

    private final TableColumnAdjuster tca;
    private FeatureController featureController;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private java.util.List<dreamwar.model.POJO.Feature> featureList;
    private javax.persistence.Query featureQuery;
    private javax.swing.JTable featureTable;
    private javax.swing.JScrollPane featureTableScrollPane;
    private javax.swing.JPanel keypadPanel;
    private javax.swing.JButton modifyButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
